package com.base.study.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.study.R;
import com.base.study.adapter.OnlineStudyRecyclerAdapter;
import com.base.study.bean.SubjectListBean;
import com.base.study.databinding.ActivityCurriculumDetailBinding;
import com.base.study.databinding.ActivitySubjectListBinding;
import com.base.study.presenter.SubjectListPresenter;
import com.base.study.view.SubjectListView;
import com.base.study.widget.RecyclerViewSpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * 试卷列表  废弃
 */
@Route(path = "/sublect/list_back")
public class SubjectListActivity extends CBaseBarMvpActivity<SubjectListPresenter> implements SubjectListView {
    private ActivitySubjectListBinding mBinding;
    private int page = 1;
    private List<SubjectListBean.Rows> mData=new ArrayList<>();
    private BaseAdapter<SubjectListBean.Rows> subjectListBeanBaseAdapter;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_subject_list;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivitySubjectListBinding) mRootBinding;
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        mPresenter = new SubjectListPresenter(this, this);
    }

    @Override
    protected void start() {
        setBackNavigation();
        setBarTitle("抽取试卷");
        mPresenter.getList(1, 10);
        subjectListBeanBaseAdapter = new BaseAdapter<SubjectListBean.Rows>(mData, mContext) {
            @Override
            public void convert(BaseHolder holder, final SubjectListBean.Rows subjectListBean, boolean isHeader, boolean isBottom) {
                TextView title = holder.getView(R.id.item_subject_title);
                TextView number = holder.getView(R.id.item_number);
                TextView result = holder.getView(R.id.item_look_result);
//
//                if (!TextUtils.isEmpty(subjectListBean.getTestQuestions())){
//                    title.setText(subjectListBean.getTestQuestions());
//                }else {
//                    title.setText("暂无");
//                }
//
//                if (!TextUtils.isEmpty(subjectListBean.getParentId())){
//                    number.setText(String.valueOf(subjectListBean.getId()));
//                }else {
//                    number.setText("暂无");
//                }



                result.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        ARouter.getInstance().build("/subject/subject").withString("formId",subjectListBean.getFormId()).navigation();
                    }
                });

            }
        };

        subjectListBeanBaseAdapter.putNormalType(R.layout.item_subjectlist);
        subjectListBeanBaseAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                SubjectListBean.Rows rows=mData.get(dataPosition);

                //ARouter.getInstance().build("/lpark/parkdetail").withParcelable("mPark",illegalPark).withInt("mType",1).navigation();
            }
        });
        mBinding.rvSubjectList.setAdapter(subjectListBeanBaseAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rvSubjectList.setLayoutManager(linearLayoutManager);
        mBinding.rvSubjectList.addItemDecoration(new RecyclerViewSpacesItemDecoration
                (10, 10, 10, 10));

        mBinding.smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                mPresenter.getList(page, 10);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                mPresenter.getList(page, 10);
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }

    @Override
    public void returnNoData() {
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.empty.setVisibility(View.VISIBLE);
    }

    @Override
    public void returnList(SubjectListBean bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.clear();
        mData.addAll(bean.getRows());
        subjectListBeanBaseAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnMoreList(SubjectListBean bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.addAll(bean
                .getRows());
        subjectListBeanBaseAdapter.notifyDataSetChanged();

    }

    @Override
    public void returnNoMoreData() {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(true);
    }

    @Override
    public void returnError() {
        mBinding.empty.setVisibility(View.GONE);
        if (page == 1) {
            mBinding.smartRefreshLayout.finishRefresh();
        } else {
            mBinding.smartRefreshLayout.finishLoadmore();
        }
    }
}
